package ad.utils 
{
	/**
	 * TypeFileUtil
	 * @version ActionScript 3.0
	 * @author Adrian C. Miranda
	 * @mail ad@adrianmiranda.com.br
	 */
	public class TypeFileUtil
	{
		private static const TEXT_EXTENSIONS:Array = ['xml', 'js', 'php', 'asp', 'txt', 'py'];
		private static const IMAGE_EXTENSIONS:Array = ['jpg', 'jpeg', 'gif', 'png'];
		private static const VIDEO_EXTENSIONS:Array = ['flv', 'f4v', 'f4p', 'mp4'];
		private static const SOUND_EXTENSIONS:Array = ['mp3', 'f4a', 'f4b'];
		private static const ALL_EXTENSIONS:Array = [TEXT_EXTENSIONS, IMAGE_EXTENSIONS, VIDEO_EXTENSIONS, SOUND_EXTENSIONS];
		
		public static function exec(input:String):String
		{
			var mimetypes:Array = new Array();
			for (var id:int = 0; id < ALL_EXTENSIONS.length; id++)
			{
				mimetypes[id] = ALL_EXTENSIONS[id].join('|');
			}
			var extensions:String = ALL_EXTENSIONS.join('|');
			var regularExpression:RegExp = new RegExp('.*\\.(['+ extensions +']*)', 'i');
			var output:Object = regularExpression.exec(input);
			return (output[1].length > 1) ? output[1] : null;
		}
		
		public static function isText(typeOfFile:String):Boolean
		{
			for (var id:int = 0; id < TEXT_EXTENSIONS.length; id++) 
			{
				if (typeOfFile === TEXT_EXTENSIONS[id]) return true;
			}
			return false;
		}
		
		public static function isImage(typeOfFile:String):Boolean
		{
			for (var id:int = 0; id < IMAGE_EXTENSIONS.length; id++) 
			{
				if (typeOfFile === IMAGE_EXTENSIONS[id]) return true;
			}
			return false;
		}
		
		public static function isVideo(typeOfFile:String):Boolean
		{
			for (var id:int = 0; id < VIDEO_EXTENSIONS.length; id++) 
			{
				if (typeOfFile === VIDEO_EXTENSIONS[id]) return true;
			}
			return false;
		}
		
		public static function isSound(typeOfFile:String):Boolean
		{
			for (var id:int = 0; id < SOUND_EXTENSIONS.length; id++) 
			{
				if (typeOfFile === SOUND_EXTENSIONS[id]) return true;
			}
			return false;
		}
		
		public static function isValidExtension(typeOfFile:String):Boolean
		{
			for (var id:int = 0; id < ALL_EXTENSIONS.length; id++) 
			{
				//ALL_EXTENSIONS[id] = ALL_EXTENSIONS[id].split('|');
				
				for (var uI:int = 0; uI < ALL_EXTENSIONS[id].length; uI++) 
				{
					if (typeOfFile == ALL_EXTENSIONS[id][uI]) return true;
				}
			}
			return false;
		}
		
	}

}